package com.gx.smart.building.ui.common.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaosy
 * 时间: 2022/2/15
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public abstract class BaseAdapterWithDiff<E, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    protected Context mContext;
    protected BaseAdapter.OnItemClickListener mItemClickListener;
    protected BaseAdapter.OnItemLongClickListener mOnItemLongClickListener;
    private final AsyncListDiffer<E> mDiffer;

    public BaseAdapterWithDiff(Context context, @NonNull DiffUtil.ItemCallback<E> diffCallback) {
        mContext = context;
        mDiffer = new AsyncListDiffer(this, diffCallback);
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(onBindLayout(), parent, false);
        return onCreateHolder(view);
    }

    //绑定布局文件
    protected abstract int onBindLayout();

    //创建一个holder
    protected abstract VH onCreateHolder(View view);

    //绑定数据
    protected abstract void onBindData(VH holder, E e, int position);

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        final E e = getItem(position);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(view -> mItemClickListener.onItemClick(e, position));
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(view -> mOnItemLongClickListener.onItemLongClick(e, position));
        }
        onBindData(holder, e, position);
    }

    @Override
    public int getItemCount() {
        if( mDiffer.getCurrentList() == null){
            return 0;
        }
        return mDiffer.getCurrentList().size();
    }

    public void submitList(List<E> data) {
        if (data != null && data.size() > 0) {
            mDiffer.submitList(new ArrayList<>(data));
        }
    }

    public E getItem(int position) {
        if (position < 0 || position >= getItemCount())
            throw new IllegalArgumentException("index out of list position:" + position);
        return mDiffer.getCurrentList().get(position);
    }

    public void setItemClickListener(BaseAdapter.OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setOnItemLongClickListener(BaseAdapter.OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }



    public interface OnItemClickListener<E> {
        void onItemClick(E e, int position);
    }

    public interface OnItemLongClickListener<E> {
        boolean onItemLongClick(E e, int position);
    }
}
